package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.ElderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人表Service业务层处理
 *
 * @author Tomorrow
 * @date 2025-01-02
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人表
     *
     * @param id 老人表主键
     * @return 老人表
     */
    @Override
    public Elder selectElderById(Long id) {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人表列表
     *
     * @param elder 老人表
     * @return 老人表
     */
    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人表
     *
     * @param elder 老人表
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder) {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人表
     *
     * @param elder 老人表
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder) {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人表
     *
     * @param ids 需要删除的老人表主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids) {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人表信息
     *
     * @param id 老人表主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id) {
        return elderMapper.deleteById(id);
    }

    /**
     * 分页查询老人数据列表
     *
     * @param elderDto
     * @return
     */
    @Override
    public TableDataInfo<Elder> pageQuery(ElderDto elderDto) {
        LambdaQueryWrapper<Elder> queryWrapper = Wrappers.<Elder>lambdaQuery().eq(Elder::getStatus, elderDto.getStatus())
                .like(StringUtils.isNotEmpty(elderDto.getName()), Elder::getName, elderDto.getName())
                .like(StringUtils.isNotEmpty(elderDto.getIdCardNo()), Elder::getIdCardNo, elderDto.getIdCardNo());

        //创建分页对象
        Page<Elder> page = new Page<>(elderDto.getPageNum(), elderDto.getPageSize());
        page = page(page, queryWrapper);

        return getTableData(page);

    }

    //封装查询结果
    private TableDataInfo<Elder> getTableData(Page<Elder> page) {

        //创建TableDataInfo对象
        TableDataInfo<Elder> elderTableDataInfo = new TableDataInfo<>();

        elderTableDataInfo.setRows(page.getRecords());
        elderTableDataInfo.setTotal(page.getTotal());
        elderTableDataInfo.setCode(HttpStatus.SUCCESS);
        elderTableDataInfo.setMsg("查询成功");

        return elderTableDataInfo;




    }
}
